<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div>
        <!-- 搜索 -->
        <el-input v-model="query.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <el-select v-model="statusType" clearable placeholder="状态" class="filter-item" style="width: 130px">
        <el-option
          v-for="item in statusOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
	  <el-select v-model="query.businessId" clearable placeholder="商户列表" class="filter-item" filterable style="width: 200px" @change="crud.toQuery">
	    <el-option v-for="item in businessNameList" 
	      :value="item.id"
	      :key="item.id"
	      :label="item.businessName">
	    </el-option>
	  </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <!-- <crudOperation :permission="permission" /> -->
      <el-button
          v-permission="['admin','syUserTester:add']"
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-plus"
          @click="crud.toAdd"
        >新增</el-button>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="1000px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <el-row>
            <el-col class="query-class">
              <el-form-item label="选择用户" prop="userName">
                <el-input v-model="form.userName" placeholder="请输入会员账号" style="width:370px;" clearable :disabled="nameDisabled"/>
              </el-form-item>
              <el-form-item style="margin-left: -75px;!important;">
                <el-input v-model="form.uid" v-show="false"/>
                <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="toQuery" :disabled="nameDisabled">搜索</el-button>
              </el-form-item>
              <div class="font-class">{{msg}}</div>
            </el-col>
          </el-row>
          
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="检测昵称" prop="nickname">
                <el-input v-model="form.nickname" :disabled="isDisabledInput"/>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="手机号码" prop="phone">
                <el-input v-model="form.phone" :disabled="isDisabledInput"/>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="状态" prop="status">
                <el-radio-group v-model="form.status" :disabled="isDisabledInput">
                  <el-radio label="1">正常</el-radio>
                  <el-radio label="0">禁止</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="微信号" prop="wechatNumber">
                <el-input v-model="form.wechatNumber" :disabled="isDisabledInput"/>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="工作经验" prop="workExperience">
                <el-input v-model="form.workExperience" type="number" :disabled="isDisabledInput"/>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="服务星级" prop="serviceStar">
                <el-input v-model="form.serviceStar" type="number" :disabled="isDisabledInput"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="检测头像">
              <MaterialList
                      v-model="avatarArr"
                      style="width: 915px;"
                      type="image"
                      :num="1"
                      :width="150"
                      :height="150"
                      :disabled="isDisabledInput"
              />
          </el-form-item>
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="工作时间" prop="workingHours">
                <el-input v-model="form.workingHours" :disabled="isDisabledInput"/>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="寄存/回收单权限" prop="isSelling">
                <el-radio-group v-model="form.isSelling" :disabled="isDisabledInput">
                  <el-radio label="0">否</el-radio>
                  <el-radio label="1">是</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="报价回复权限" prop="isOffer">
                <el-radio-group v-model="form.isOffer" :disabled="isDisabledInput">
                  <el-radio label="0">否</el-radio>
                  <el-radio label="1">是</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" v-if="false"/>
        <el-table-column v-if="columns.visible('userName')" prop="userName" label="用户号" show-overflow-tooltip />
        <el-table-column v-if="columns.visible('nickname')" prop="nickname" label="检测师昵称" show-overflow-tooltip />
        <el-table-column v-if="columns.visible('avatar')" prop="avatar" label="头像" width="80">
          <template slot-scope="scope">
            <a :href="scope.row.avatar" style="color: #42b983" target="_blank">
              <img :src="scope.row.avatar" alt="点击打开" class="table-img" />
            </a>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('phone')" prop="phone" label="手机号码" width="100"/>
        <el-table-column v-if="columns.visible('wechatNumber')" prop="wechatNumber" label="微信号" show-overflow-tooltip width="100"/>
        <el-table-column v-if="columns.visible('workExperience')" prop="workExperience" label="工作经验" width="80"/>
        <el-table-column v-if="columns.visible('serviceStar')" prop="serviceStar" label="服务星级" width="80"/>
		<el-table-column v-if="columns.visible('businessName')" prop="businessName" label="所属招商" width="80" show-overflow-tooltip/>
        <el-table-column v-if="columns.visible('workingHours')" prop="workingHours" label="工作时间" show-overflow-tooltip />
        <el-table-column v-if="columns.visible('status')" prop="status" label="状态" width="60" >
          <template slot-scope="scope">
            <div>
              <el-tag v-if="scope.row.status == 1" :type="''">正常</el-tag>
              <el-tag v-else :type=" 'info' ">禁用</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('isSelling')" prop="isSelling" label="寄存/回收单权限" width="110" >
          <template slot-scope="scope">
            <div>
              <el-tag v-if="scope.row.isSelling == 1" :type="''">是</el-tag>
              <el-tag v-else :type=" 'info' ">否</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('isOffer')" prop="isOffer" label="报价回复权限" width="100" >
          <template slot-scope="scope">
            <div>
              <el-tag v-if="scope.row.isOffer == 1" :type="''">是</el-tag>
              <el-tag v-else :type=" 'info' ">否</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column v-permission="['admin','syUserTester:edit','syUserTester:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudSyUserTester from '@/api/syUserTester'
import { getUserByName } from '@/api/syUserTester'
import CRUD, { presenter, header, form, crud } from '@crud/cruid'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UID.operation'
import pagination from '@crud/Pagination'
import MaterialList from "@/components/material";
import { getBusinessNameList } from '@/api/syBusinessName'

// crud交由presenter持有
const defaultCrud = CRUD({ title: '检测师', url: 'api/syUserTester', sort: 'uid,desc', crudMethod: { ...crudSyUserTester }})
const defaultForm = { uid: null, nickname: null, avatar: null, phone: null, wechatNumber: null, workExperience: 3, serviceStar: 5, workingHours: '早上 09:00 - 晚上 21:00', status: '1',isSelling:'0',isOffer:'0', createTime: null, updateTime: null, userName: null }
export default {
  name: 'SyUserTester',
  components: { pagination, crudOperation, rrOperation, udOperation ,MaterialList},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  dicts: ['is_enable'],
  data() {
    return {
      statusType: '',
      statusOptions: [
        { value: '1', label: '正常' },
        { value: '0', label: '禁止' }
      ],
      isSellingOptions: [
        { value: '1', label: '是' },
        { value: '0', label: '否' }
      ],
      msg:'',
      isDisabledInput:true,
      nameDisabled:false,
      avatarArr:[],
      permission: {
        add: ['admin', 'syUserTester:add'],
        edit: ['admin', 'syUserTester:edit'],
        del: ['admin', 'syUserTester:del']
      },
      rules: {
        nickname: [
          { required: true, message: '检测师昵称不能为空', trigger: 'blur' }
        ],
        avatar: [
          { required: true, message: '检测师头像不能为空', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '手机号码不能为空', trigger: 'blur' }
        ],
        wechatNumber: [
          { required: true, message: '微信号不能为空', trigger: 'blur' }
        ],
        workExperience: [
          { required: true, message: '工作经验不能为空', trigger: 'blur' }
        ],
        serviceStar: [
          { required: true, message: '服务星级不能为空', trigger: 'blur' }
        ],
        workingHours: [
          { required: true, message: '工作时间不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '1为正常，0为禁止不能为空', trigger: 'blur' }
        ],
        userName: [
          { required: true, message: '用户名(账号)不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'nickname', display_name: '检测师昵称' },
        { key: 'phone', display_name: '手机号码' },
        { key: 'wechatNumber', display_name: '微信号' },
        { key: 'userName', display_name: '用户名(账号)' }
      ],
	  businessNameList: [],//商户列表
    }
  },
  watch: {
    "avatarArr":function(val) {
      if (val) {
        this.form.avatar = val.join(",");
      }
    },
  },
  created() {
    let that = this;
    setTimeout(function() {
  	   that.getBusinessListInit();
    }, 500);
  },
  methods: {
	getBusinessListInit(){
	    getBusinessNameList().then(res => {
	      this.businessNameList = res;
	    })
	},
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
        if(this.statusType){
          this.crud.params.status = this.statusType;
        }else{
          delete this.crud.params.status
        }
      }else{
        delete this.crud.params.nickname
        delete this.crud.params.phone
        delete this.crud.params.wechatNumber
        delete this.crud.params.userName
        delete this.crud.params.businessId
        this.statusType ='';
      }
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      this.form.status = form.status.toString();
      this.form.isSelling = form.isSelling.toString();
      this.form.isOffer = form.isOffer.toString();
      this.msg = '';
      if (form.avatar && form.uid) {
        this.avatarArr = form.avatar.split(",");
        this.isDisabledInput = false;
        this.nameDisabled = true;
      }else{    
        this.isDisabledInput = true;
        this.nameDisabled = false;
        this.avatarArr=[]
      }
    },
    toQuery(){
      let userName = this.form.userName;
      if(userName){
        getUserByName({userName:userName}).then(res => {
          if(res.code =='0'){
            this.isDisabledInput = false;
            this.nameDisabled = true;
            this.form.phone = res.yxUser.phone;
            this.form.uid = res.yxUser.uid;
            this.form.nickname = res.yxUser.nickname;
            this.avatarArr = res.yxUser.avatar.split(",");
          }else if(res.code =='2'){//说明已存在
              this.msg = '该会员账号已存在！请检查…';
              this.isDisabledInput = true;
              this.nameDisabled = false;
          }else{
              this.msg = '该会员账号不存在！请检查…';
              this.isDisabledInput = true;
              this.nameDisabled = false;
          }
          
        })
      }else{
        this.msg = '';
        this.isDisabledInput = true;
        this.nameDisabled = false;
        this.$message({
          message: '请输入搜索会员账号！',
          type: 'warning'
        });
      }  
    }
  }
}

</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
  .query-class{
    display: flex;
  }
  .font-class{
    color: red;
    margin-top: 5px;
    font-weight: 500;
    margin-left: 5px;
  }
</style>
